﻿using DiffuseEnjoyIdle.Core.Enums;
using DiffuseEnjoyIdle.Core.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace System.Linq
{
    /// <summary>
    /// inc查询 数组中的每个值模糊查询以此开头
    /// </summary>
    public sealed class ExpBuilderIncMulltilStartWith : ExpressionBuilder, IExpressionBuilder
    {
        private static readonly MethodInfo MatchInfo;

        public ExpBuilderIncMulltilStartWith()
        {
            base.OpType = Enum_ConditionType.IncMulStartWith;
            base.EndKey = Enum_ConditionType.IncMulStartWith.ToString();
        }

        static ExpBuilderIncMulltilStartWith()
        {
            Type typeFromHandle = typeof(string);
            MatchInfo = typeFromHandle.GetMethod(Enum_ConditionType.IncMulStartWith.GetDescription(), new Type[] { typeFromHandle });
        }

        protected override Expression GenExp(Expression left, Expression right)
        {
            string[] array = (string[])((ConstantExpression)right).Value;
            Expression expression = null;
            foreach (string value in array)
            {
                if (!string.IsNullOrEmpty(value))
                {
                    ConstantExpression constantExpression = Expression.Constant(value);
                    var methodCallExpression = Expression.Call(left, MatchInfo, new Expression[] { constantExpression });
                    if (expression == null)
                    {
                        expression = methodCallExpression;
                    }
                    else
                    {
                        expression = Expression.Or(expression, methodCallExpression);
                    }
                }
            }
            return expression;
        }
    }
}